package com.example.e_learn;

 import android.content.Context;
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;

 import com.example.e_learn.Control.WordUtil;
 import com.example.e_learn.Entity.Word;
 import java.io.InputStream;
 import java.io.Serializable;
 import java.util.List;

/**
 *
 * 第一个界面，主要用与单词数据的准备和选择测试模式
 *
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btnToAllTest;
    private Button btnToUnitTest;
    private Button btnToFaultTest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //完成单词列表的配置
        InputStream in = null;
        in = getResources().openRawResource(R.raw.word);
        WordUtil.toLoading(in);
        //注册控件
        btnToAllTest = findViewById(R.id.all_test_button);
        btnToUnitTest = findViewById(R.id.unit_test_button);
        btnToFaultTest = findViewById(R.id.fault_test_button);
        btnToAllTest.setOnClickListener(this);
        btnToUnitTest.setOnClickListener(this);
        btnToFaultTest.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //全部测试，传入全部单词
            case R.id.all_test_button:
                actionStart(MainActivity.this,WordUtil.getAllWords());
                break;
            //单元测试，选择测试的单元传入数据
            case R.id.unit_test_button:
                unitClick();
                //actionStart(MainActivity.this,WordUtil.getAllWords());
                //获取一个随机数，为1-10
                int nm = (WordUtil.getUnitcounts() * 100 - 1) - (int) (100 * Math.random());
                break;
            //错题测试，传入测试的错题
            case R.id.fault_test_button:
                if (WordUtil.getFaultwordcounts() == 0 ) {
                    Toast.makeText(this, "您当前还没有错题哦，请先进行别的测试",
                            Toast.LENGTH_SHORT).show();
                }else{
                    actionStart(MainActivity.this,WordUtil.getAllFaultwords());
                }



        }
    }

    private void actionStart(Context context , List<Word> list) {
        Intent intent = new Intent(context,ETestActivity.class);
        intent.putExtra("words",(Serializable) list);
        startActivity(intent);
    }

    private void unitClick(){
        ShowDialog showDialog = new ShowDialog();
        showDialog.show(MainActivity.this, "单元测试", "请输入您要测试的单元", new ShowDialog.OnBottomClickListener() {
            @Override
            public void positive() {
                Toast.makeText(MainActivity.this, "Click yes", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void negative() {
                Toast.makeText(MainActivity.this, "Click no", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
